package com.ctsi.ssdc.service.impl;


import org.springframework.stereotype.Service;
import org.apache.commons.collections.CollectionUtils;
import com.ctsi.ssdc.domain.LitemallSearchHistory;
import com.ctsi.ssdc.domain.LitemallSearchHistoryExample;
import com.ctsi.ssdc.service.LitemallSearchHistoryService;
import com.ctsi.ssdc.repository.LitemallSearchHistoryRepository;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Objects;
import com.ctsi.ssdc.service.impl.StrengthenBaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import com.ctsi.ssdc.model.PageResult;
import org.springframework.data.domain.Pageable;
import com.github.pagehelper.PageHelper;
/**
 * Service Implementation for managing LitemallSearchHistory.
 *
 * @author ctsi-biyi-generator
 *
 */
@Service
public class LitemallSearchHistoryServiceImpl 
	extends StrengthenBaseServiceImpl<LitemallSearchHistoryRepository, LitemallSearchHistory, Integer, LitemallSearchHistoryExample> 
	implements LitemallSearchHistoryService {

    @Autowired
    LitemallSearchHistoryRepository litemallSearchHistoryRepository;


    /**
    * GET  /litemallSearchHistorys : get the litemallSearchHistorys firstStringBaseColumn.
    */
    @Override
    public PageResult<LitemallSearchHistory> findFirstStringColumn(String keyword,Pageable pageable){
        String str = keyword;
        if (Objects.nonNull(pageable)) {
            PageHelper.startPage(pageable.getPageNumber() + 1, pageable.getPageSize());
        }
        LitemallSearchHistoryExample litemallSearchHistoryExample = new LitemallSearchHistoryExample();
        String orderBy = getPageOrderBy(pageable);
        if(StringUtils.isNotEmpty(orderBy)) {
            litemallSearchHistoryExample.setOrderByClause(orderBy);
        }
        if (StringUtils.isEmpty(str)){
            litemallSearchHistoryExample.createCriteria();
        } else{
            litemallSearchHistoryExample.createCriteria().andKeywordLike("%" + str +"%");
        }
        List<LitemallSearchHistory>  data = litemallSearchHistoryRepository.selectByExample(litemallSearchHistoryExample);

        long count = 0L;
        if (CollectionUtils.isNotEmpty(data)){
            count = litemallSearchHistoryRepository.countByExample(litemallSearchHistoryExample);
        }
        return new PageResult<LitemallSearchHistory>(data,count,count);
    }

    @Override
    public List<LitemallSearchHistory> queryByUid(Integer userId) {
        LitemallSearchHistoryExample example = new LitemallSearchHistoryExample();
        example.or().andUserIdEqualTo(userId).andDeletedEqualTo(false);
        example.setDistinct(true);
        return litemallSearchHistoryRepository.selectByExampleSelective(example, LitemallSearchHistory.Column.keyword);
    }

    @Override
    public void deleteByUid(Integer userId) {
        LitemallSearchHistoryExample example = new LitemallSearchHistoryExample();
        example.or().andUserIdEqualTo(userId);
        litemallSearchHistoryRepository.logicalDeleteByExample(example);
    }

    @Override
    public void save(LitemallSearchHistory searchHistoryVo) {
        searchHistoryVo.setAddTime(ZonedDateTime.now());
        searchHistoryVo.setUpdateTime(ZonedDateTime.now());
        litemallSearchHistoryRepository.insertSelective(searchHistoryVo);
    }


    private String getPageOrderBy(Pageable page) {
        if(page!= null && page.getSort() != null) {
            StringBuilder sb = new StringBuilder();
            page.getSort().forEach(sort -> sb.append(sort.getProperty())
            .append(" ").append(sort.getDirection()).append(","));
            if(sb.length() > 1) {
                return (sb.substring(0,sb.length()-1));
             }
        }
        return null;
    }
}
